// // 显示成功提示
// Message.show('success', '操作成功!');
// // 显示错误提示（自定义持续时间）
// Message.show('error', '发生错误!', 5000);
// // 显示警告提示
// Message.show('warning', '警告信息');
const Message = (() => {
    let container = null;
    let messages = [];
    const iconMap = {
        success: '√',
        error: '×',
        warning: '!'
    };
    const colorMap = {
        success: '#52c41a',
        error: '#ff4d4f',
        warning: '#faad14'
    };

    function createContainer() {
        if (!container) {
            container = document.createElement('div');
            container.className = 'message-container';
            document.body.appendChild(container);
        }
        return container;
    }

    function createMessage(type, content) {
        const message = document.createElement('div');
        message.className = 'message-item';

        // 创建图标
        const icon = document.createElement('span');
        icon.className = 'message-icon';
        icon.style.color = colorMap[type];
        icon.textContent = iconMap[type];

        // 创建内容
        const contentEl = document.createElement('span');
        contentEl.className = 'message-content';
        contentEl.textContent = content;

        // 创建关闭按钮
        const close = document.createElement('span');
        close.className = 'message-close';
        close.innerHTML = '×';
        close.onclick = () => removeMessage(message);

        message.appendChild(icon);
        message.appendChild(contentEl);
        message.appendChild(close);

        return message;
    }

    function removeMessage(message) {
        message.classList.add('leaving');
        setTimeout(() => {
            const index = messages.indexOf(message);
            if (index > -1) {
                messages.splice(index, 1);
                container.removeChild(message);
                updatePositions();
            }
        }, 300);
    }

    function updatePositions() {
        messages.forEach((msg, index) => {
            msg.style.top = `${index * 60 + 20}px`;
        });
    }

    return {
        show: (type, content, duration = 3000) => {
            const container = createContainer();
            const message = createMessage(type, content);

            messages.push(message);
            container.appendChild(message);
            updatePositions();

            const timer = setTimeout(() => {
                removeMessage(message);
            }, duration);

            // 鼠标悬停暂停倒计时
            message.addEventListener('mouseenter', () => {
                clearTimeout(timer);
            });

            message.addEventListener('mouseleave', () => {
                setTimeout(() => {
                    removeMessage(message);
                }, duration);
            });
        }
    };
})();

// 暴露API
window.showMessage = (type, content) => {
    Message.show(type, content);
};